package com.gljr.autotest.testrule;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.openqa.selenium.WebDriver;

import com.gljr.autotest.tool.SeleniumUtils;

public class TakeScreenshotOnFailureRule implements TestRule {

	final WebDriver driver;

	public TakeScreenshotOnFailureRule(WebDriver driver) {
		this.driver = driver;
	}

	@Override
	public Statement apply(final Statement base, Description description) {
		return new Statement() {
			@Override
			public void evaluate() throws Throwable {
				try {
					base.evaluate();
				} catch (Throwable throwable) {
					SeleniumUtils.commonScreenShoot(driver);
					throw throwable;
				}
			}
		};
	}
}
